/*
 * @(#)DiaryController.java 2019年12月20日 下午2:32:25
 * Copyright 2019 zmr, Inc. All rights reserved. 
 * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.wind.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.code.kaptcha.Constants;
import com.wind.base.BaseController;
import com.wind.base.Page;
import com.wind.base.R;
import com.wind.domain.Diary;
import com.wind.service.IDiaryService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>File：DiaryController.java</p>
 * <p>Title: </p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2019 2019年12月20日 下午2:32:25</p>
 * <p>Company: zmrit.com </p>
 * @author zmr
 * @version 1.0
 */
@RestController
@RequestMapping("/")
public class DiaryController extends BaseController
{
    @Autowired
    private IDiaryService diaryService;

    @PostMapping("save")
    public R save(Diary diary, HttpServletRequest request, String captcha)
    {
        Object code = request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        request.getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
        if (StrUtil.isNotBlank(captcha) && captcha.equalsIgnoreCase(String.valueOf(code)))
        {
            return toAjax(diaryService.insert(diary));
        }
        else
        {
            return R.error("验证码错误");
        }
    }

    @GetMapping("random/{cate}")
    public R radnom(@PathVariable(value = "cate") Integer cate)
    {
        return R.data(diaryService.selectDiaryRandom(cate));
    }

    @GetMapping("list")
    public R list(Diary diary, Page page)
    {
        List<Diary> list = diaryService.selectDiaryList(diary);
        return result(list);
    }
}
